package cycling.taxi.com.ui.activity;

import android.content.Intent;
import android.drm.DrmStore;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.HistoryInfo;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.parse.ParseTraject;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;
import cycling.taxi.com.view.statistical.BarCharts;
import cycling.taxi.com.zxing.android.Intents;

public class TrajectoryList extends AppCompatActivity implements View.OnClickListener,RequestCallback,OnChartValueSelectedListener,OnChartGestureListener{

    private BarChart mBarChart1;
    private BarCharts mBarCharts;
    private ImageView back;
    private List<HistoryInfo> data;
    //private String[] color = {"#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD", "#C4FF8E", "#FFF88D", "#FFD38C", "#8CEBFF", "#FF8F9D", "#6BF3AD"};
    private TextView tv_licheng;
    private TextView tv_yongshi;
    private TextView tv_pju;

    private TextView maxspeed;
   // private Button gotot;
    HistoryInfo history;
    String historyinfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trajectory_list);
        initData();
        DrawTrajectory();

    }

    private void initData() {
        mBarCharts = new BarCharts();
        mBarChart1 = (BarChart) findViewById(R.id.spreadBarChart1);
    //    gotot=(Button)findViewById(R.id.gotot);
        tv_licheng=(TextView)findViewById(R.id.tv_licheng);
        tv_yongshi=(TextView)findViewById(R.id.tv_yongshi);
        tv_pju=(TextView)findViewById(R.id.tv_pju);
        maxspeed=(TextView)findViewById(R.id.maxspeed);
        XAxis xAxis = mBarChart1.getXAxis();

        xAxis.setEnabled(true);
        YAxis yAxis = mBarChart1.getAxisLeft();
        YAxis yAxis2 = mBarChart1.getAxisRight();
        yAxis.setEnabled(true);
        yAxis2.setEnabled(true);
        yAxis.setDrawGridLines(false);
        xAxis.setLabelsToSkip(0);
     //   yAxis.setLabelCount(20,false);

      //  xAxis.

  //      mBarCharts.showBarChart(mBarChart1, getBarData(20, 199), true);
        back =(ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
   //     gotot.setOnClickListener(this);
        mBarChart1.setOnChartValueSelectedListener(this);
        mBarChart1.setOnChartGestureListener(this);
       // mBarChart1.setVisibleXRangeMinimum(1f);

    }
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            finish();
        }
//        if(v.getId()==R.id.gotot){
//            Intent trajectory = new Intent(TrajectoryList.this, TestActivity.class);
//            trajectory.putExtra("history",historyinfo);
//            startActivity(trajectory);
//
//        }
    }


    /**
     * 这个方法是初始化数据的
     *
     * @param count X 轴的个数
     */
    public BarData getBarData(int count,List<HistoryInfo> data) {

        ArrayList<String> xValues = new ArrayList<>();
        for (int i = 0; i < count; i++) {
           // xValues.add("" + (i + 1) + "");// 设置每个柱壮图的文字描述
            xValues.add(data.get(i).getTime_durtion().substring(5,11));
        }

        ArrayList<BarEntry> yValues = new ArrayList<>();
        for (int i = 0; i < count; i++) {
           // float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
            float value= Float.parseFloat(data.get(i).getLicheng());
            yValues.add(new BarEntry(value, i));
        }
        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, "测试图");
        barDataSet.setBarSpacePercent(80/count);

        ArrayList<Integer> colors = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            colors.add(Color.parseColor("#ffcc0000"));
  //          colors.add(Color.parseColor(color[i]));
        }
        barDataSet.setColors(colors);
        // 设置栏阴影颜色
        barDataSet.setBarShadowColor(Color.parseColor("#01000000"));
        ArrayList<BarDataSet> barDataSets = new ArrayList<>();
        barDataSets.add(barDataSet);
        barDataSet.setValueTextColor(Color.parseColor("#FF8F9D"));
        // 绘制值
        barDataSet.setDrawValues(true);
        BarData barData = new BarData(xValues, barDataSets);
        return barData;
    }

    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        LogUtil.e("requestSuccess数据="+bean.beanJson);
        try {
            data= new Gson().fromJson(bean.beanJson,new TypeToken<List<HistoryInfo>>(){}.getType());

            if(data.size()<20) {
                mBarChart1.setVisibleXRangeMinimum(1f);
                mBarCharts.showBarChart(mBarChart1, getBarData(data.size(), data), true);
                LogUtil.e(String.valueOf(data.size()));

            }
            else {
                mBarCharts.showBarChart(mBarChart1, getBarData(20, data), true);

            }
            if(data!=null){
                tv_licheng.setText(data.get(0).getLicheng());
                tv_yongshi.setText(data.get(0).getUseTime());
                tv_pju.setText(data.get(0).getSpeed());
                maxspeed.setText(data.get(0).getMaxSpeed());
                history=data.get(0);
                String json=new Gson().toJson(history,HistoryInfo.class);
                historyinfo=json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        LogUtil.e("成功");
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {
        LogUtil.e("失败");

        ToastUtil.getInstance().toast("查询轨迹失败");

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }
    private void DrawTrajectory() {
        //获取服务器数据
        String mobile= SPUtil.getInstance().getData("phone");
        String password= SPUtil.getInstance().getData("password");
        RequestParams params = new RequestParams.Builder()
                .putParam("mobile",mobile)
                .putParam("password", MD5Util.MD5Encode(password))
                .putParam("beanJson",SPUtil.getInstance().getData("beanJson"))
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getTrajectoryList();//轨迹的url路径
        RequestUtil.requestData(requestQueue,
                IConstants.TRAJECTORY,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);

    }

    @Override
    public void onValueSelected(Entry e, int i, Highlight highlight) {

        tv_licheng.setText(data.get(e.getXIndex()).getLicheng());
        tv_yongshi.setText(data.get(e.getXIndex()).getUseTime());
        tv_pju.setText(data.get(e.getXIndex()).getSpeed());
        maxspeed.setText(data.get(e.getXIndex()).getMaxSpeed());
        history=data.get(e.getXIndex());
        String json=new Gson().toJson(history,HistoryInfo.class);
        historyinfo=json;
    }


    @Override
    public void onNothingSelected() {

    }

    @Override
    public void onChartGestureStart(MotionEvent motionEvent, ChartTouchListener.ChartGesture chartGesture) {

    }

    @Override
    public void onChartGestureEnd(MotionEvent motionEvent, ChartTouchListener.ChartGesture chartGesture) {

    }

    @Override
    public void onChartLongPressed(MotionEvent motionEvent) {

    }

    @Override
    public void onChartDoubleTapped(MotionEvent motionEvent) {
        Intent trajectory = new Intent(TrajectoryList.this, TestActivity.class);
        trajectory.putExtra("history",historyinfo);
        startActivity(trajectory);
    }

    @Override
    public void onChartSingleTapped(MotionEvent motionEvent) {

    }

    @Override
    public void onChartFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {

    }

    @Override
    public void onChartScale(MotionEvent motionEvent, float v, float v1) {

    }

    @Override
    public void onChartTranslate(MotionEvent motionEvent, float v, float v1) {

    }
}
